﻿using Common.EF;
using FCP.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FCP.Api.Utils
{
    public class EntityToWebModelUtls
    {
        public async static Task<List<t_label>> GetLablesAsync(List<string> labelIds,
                List<t_label_detail> labelDetails, ICommonRepository dataAccess)
        {
            List<t_label> labels = new List<t_label>();
            labelIds = labelIds.Where(t => !string.IsNullOrEmpty(t)).ToList();
            if (!labelIds.Any()) return labels;
            var tempLabels = await dataAccess.GetEntitiesAsync<t_label>(labelIds);
            if (tempLabels.Any())
            {
                var tempLabelDetails = await dataAccess.GetEntitiesAsync<t_label_detail>(
                        t => labelIds.Contains(t.label_id));
                labels.AddRange(tempLabels);
                labelDetails.AddRange(tempLabelDetails);
            }
            return labels;
        }

        public static async Task GetTableLabelsAsync(List<t_table> tables, string lang,
            ICommonRepository dataAccess)
        {
            if (!tables.Any()) return;
            List<string> labelIds = new List<string>();
            tables.ForEach(t =>
            {
                if (!labelIds.Contains(t.label_label_id))
                    labelIds.Add(t.label_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var table in tables)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == table.label_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    table.vir_label = l.label;
            }
        }
        public static async Task GetTableFieldLabelsAsync(List<t_table_field> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.field_label_id))
                    labelIds.Add(t.field_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.field_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_field_label = l.label;
            }
            var tablePanelIds = tableFields.Where(t => !string.IsNullOrEmpty(t.table_panel_id)).Select(t => t.table_panel_id).ToList();
            if (tablePanelIds.Any())
            {
                var tp = await dataAccess.GetEntitiesAsync<t_table_panel>(tablePanelIds);
                await GetTablePanelLabelsAsync(tp.ToList(), lang, dataAccess);
                foreach (var field in tableFields)
                {
                    var p = tp.FirstOrDefault(t => t.id == field.table_panel_id);
                    if (p != null)
                        field.vir_table_panel_label = p.vir_label;
                }
            }
        }
        public static async Task GetTablePanelLabelsAsync(List<t_table_panel> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.label_label_id))
                    labelIds.Add(t.label_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.label_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_label = l.label;
            }
        }
        public static async Task GetTableBtnLabelsAsync(List<t_table_btn> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.btn_label_id))
                    labelIds.Add(t.btn_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.btn_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_btn_label = l.label;
            }
        }


        public static async Task GetEditLabelsAsync(List<t_edit> tables, string lang,
            ICommonRepository dataAccess)
        {
            if (!tables.Any()) return;
            List<string> labelIds = new List<string>();
            tables.ForEach(t =>
            {
                if (!labelIds.Contains(t.label_label_id))
                    labelIds.Add(t.label_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var table in tables)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == table.label_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    table.vir_label = l.label;
            }
        }
        public static async Task GetEditFieldLabelsAsync(List<t_edit_field> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.field_label_id))
                    labelIds.Add(t.field_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.field_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_field_label = l.label;
            }

            var tablePanelIds = tableFields.Where(t => !string.IsNullOrEmpty(t.edit_panel_id)).Select(t => t.edit_panel_id).ToList();
            if (tablePanelIds.Any())
            {
                var tp = await dataAccess.GetEntitiesAsync<t_edit_panel>(tablePanelIds);
                await GetEditPanelLabelsAsync(tp.ToList(), lang, dataAccess);
                foreach (var field in tableFields)
                {
                    var p = tp.FirstOrDefault(t => t.id == field.edit_panel_id);
                    if (p != null)
                        field.vir_edit_panel_label = p.vir_label;
                }
            }

        }
        public static async Task GetEditPanelLabelsAsync(List<t_edit_panel> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.label_label_id))
                    labelIds.Add(t.label_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.label_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_label = l.label;
            }
        }
        public static async Task GetEditBtnLabelsAsync(List<t_edit_btn> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.btn_label_id))
                    labelIds.Add(t.btn_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.btn_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_btn_label = l.label;
            }
        }





        public static async Task GetTreeLabelsAsync(List<t_tree> tables, string lang,
            ICommonRepository dataAccess)
        {
            if (!tables.Any()) return;
            List<string> labelIds = new List<string>();
            tables.ForEach(t =>
            {
                if (!labelIds.Contains(t.label_label_id))
                    labelIds.Add(t.label_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var table in tables)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == table.label_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    table.vir_label = l.label;
            }
        }
        public static async Task GetTreeFieldLabelsAsync(List<t_tree_field> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.field_label_id))
                    labelIds.Add(t.field_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.field_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_field_label = l.label;
            }
        }

        public static async Task GetTreeBtnLabelsAsync(List<t_tree_btn> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.btn_label_id))
                    labelIds.Add(t.btn_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.btn_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_btn_label = l.label;
            }
        }




        public static async Task GetCustomLabelsAsync(List<t_custom> tables, string lang,
            ICommonRepository dataAccess)
        {
            if (!tables.Any()) return;
            List<string> labelIds = new List<string>();
            tables.ForEach(t =>
            {
                if (!labelIds.Contains(t.label_label_id))
                    labelIds.Add(t.label_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var table in tables)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == table.label_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    table.vir_label = l.label;
            }
        }

        public static async Task GetCustomBtnLabelsAsync(List<t_custom_btn> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.btn_label_id))
                    labelIds.Add(t.btn_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.btn_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_btn_label = l.label;
            }
        } 



        public static async Task GetSearchLabelsAsync(List<t_search> tables, string lang,
            ICommonRepository dataAccess)
        {
            if (!tables.Any()) return;
            List<string> labelIds = new List<string>();
            tables.ForEach(t =>
            {
                if (!labelIds.Contains(t.label_label_id))
                    labelIds.Add(t.label_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var table in tables)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == table.label_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    table.vir_label = l.label;
            }
        }

        public static async Task GetSearchFieldLabelsAsync(List<t_search_field> tableFields, string lang,
            ICommonRepository dataAccess)
        {
            if (!tableFields.Any()) return;
            List<string> labelIds = new List<string>();
            tableFields.ForEach(t =>
            {
                if (!labelIds.Contains(t.field_label_id))
                    labelIds.Add(t.field_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var field in tableFields)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == field.field_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    field.vir_field_label = l.label;
            }
        }



        public static async Task GetChartLabelsAsync(List<t_chart> tables, string lang,
            ICommonRepository dataAccess)
        {
            if (!tables.Any()) return;
            List<string> labelIds = new List<string>();
            tables.ForEach(t =>
            {
                if (!labelIds.Contains(t.label_label_id))
                    labelIds.Add(t.label_label_id);

            });
            List<t_label_detail> labelDetails = new List<t_label_detail>();
            var labels = await GetLablesAsync(labelIds, labelDetails, dataAccess);
            foreach (var table in tables)
            {
                var tempDetails = labelDetails.Where(t => t.label_id == table.label_label_id).ToList();
                var l = tempDetails.FirstOrDefault(t => string.Compare(t.lang, lang, true) == 0);
                if (l == null) l = tempDetails.OrderBy(t => t.lang).FirstOrDefault();
                if (l != null)
                    table.vir_label = l.label;
            }
        }
    }
}
